package com.anolesoft.yr.mbus.base.mapper;

import com.anolesoft.yr.mbus.base.entity.BaseRegionDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/7 16:53
 */
@Mapper
public interface BaseRegionMapper {

    @Insert("insert into base_region (" +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark" +
            ") values" +
            "(" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{parentUid,jdbcType=VARCHAR}," +
            "#{regionCode,jdbcType=VARCHAR}," +
            "#{regionLevel,jdbcType=VARCHAR}," +
            "#{regionName,jdbcType=VARCHAR}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{deleteFlag,jdbcType=BOOLEAN}," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(BaseRegionDO baseRegionDO);

    @Update("update  base_region set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}, " +
            "parent_uid = #{parentUid,jdbcType=VARCHAR}, " +
            "region_code = #{regionCode,jdbcType=VARCHAR}, " +
            "region_level = #{regionLevel,jdbcType=VARCHAR}, " +
            "region_name = #{regionName,jdbcType=VARCHAR}, " +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}, " +
            "m_time = now(), " +
            "remark =  #{remark,jdbcType=VARCHAR} " +
            "where objuid = #{objuid,jdbcType=VARCHAR}")
    int update(BaseRegionDO baseRegionDO);

    @Update("update base_region set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "BaseRegionResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "parent_uid", property = "parentUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "region_code", property = "regionCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "region_level", property = "regionLevel", jdbcType = JdbcType.VARCHAR),
            @Result(column = "region_name", property = "regionName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),

    })
    @Select("select " +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_region where del_flag = 0")
    List<BaseRegionDO> selectAll();

    @ResultMap("BaseRegionResultMap")
    @Select("select " +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_region where del_flag = 0")
    List<BaseRegionDO> selectPageAll(Page<BaseRegionDO> page);

    @ResultMap("BaseRegionResultMap")
    @Select("select " +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_region where del_flag = 0 and objuid = #{id, jdbcType=VARCHAR}")
    BaseRegionDO selectOneById(@Param("id") String id);

    @ResultMap("BaseRegionResultMap")
    @Select("select " +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_region where del_flag = 0 and objuid like #{name, jdbcType=VARCHAR}")
    List<BaseRegionDO> selectListByName(@Param("name") String name);

    @ResultMap("BaseRegionResultMap")
    @Select("select " +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_region where del_flag = 0 and objuid like #{name, jdbcType=VARCHAR}")
    List<BaseRegionDO> selectPageByName(@Param("name") String name, Page<BaseRegionDO> page);

    @ResultMap("BaseRegionResultMap")
    @Select("select " +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_region where del_flag = 0 and fac_code = #{factoryCode, jdbcType=VARCHAR}")
    List<BaseRegionDO> selectListByFactoryCode(@Param("factoryCode") String factoryCode);

    @ResultMap("BaseRegionResultMap")
    @Select("select " +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_region where del_flag = 0 and fac_code = #{factoryCode, jdbcType=VARCHAR}")
    List<BaseRegionDO> selectPageByFactoryCode(@Param("factoryCode") String factoryCode, Page<BaseRegionDO> page);

    @ResultMap("BaseRegionResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_region where del_flag = 0 " +
            "<if test =\"o.regionName !=null\">\n" +
            "   and region_name like #{o.regionName, jdbcType=VARCHAR} \n" +
            "</if>" +
            "<if test =\"o.regionCode !=null\">\n" +
            "   and region_code like #{o.regionCode, jdbcType=VARCHAR} \n" +
            "</if>" +
            "<if test =\"o.regionLevel !=null\">\n" +
            "   and region_level like #{o.regionLevel, jdbcType=VARCHAR} \n" +
            "</if>" +
            "<if test =\"o.factoryCode !=null\">\n" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} \n" +
            "</if>" +
            "</script>")
    List<BaseRegionDO> selectListByCondition(@Param("o") BaseRegionDO baseRegionDO);

    @ResultMap("BaseRegionResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_region where del_flag = 0 " +
            "<if test =\"o.regionName !=null\">\n" +
            "   and region_name like #{o.regionName, jdbcType=VARCHAR} \n" +
            "</if>" +
            "<if test =\"o.regionCode !=null\">\n" +
            "   and region_code like #{o.regionCode, jdbcType=VARCHAR} \n" +
            "</if>" +
            "<if test =\"o.regionLevel !=null\">\n" +
            "   and region_level like #{o.regionLevel, jdbcType=VARCHAR} \n" +
            "</if>" +
            "<if test =\"o.factoryCode !=null\">\n" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} \n" +
            "</if>" +
            "<if test =\"o.parentUid !=null\">\n" +
            "   and parent_uid = #{o.parentUid, jdbcType=VARCHAR} \n" +
            "</if>" +
            "</script>")
    List<BaseRegionDO> selectPageByCondition(@Param("o") BaseRegionDO baseRegionDO, Page<BaseRegionDO> page);

    @ResultMap("BaseRegionResultMap")
    @Select("select " +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_region where del_flag = 0 and parent_uid = #{parentUid, jdbcType=VARCHAR}")
    List<BaseRegionDO> findChildrenByParentUid(@Param("parentUid") String parentUid);

    @ResultMap("BaseRegionResultMap")
    @Select("select " +
            "objuid, fac_code, parent_uid, region_code, region_level, region_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_region where del_flag = 0 and parent_uid = '-'")
    List<BaseRegionDO> findTopLevel();

    @Select("select count(objuid) from base_region where del_flag = 0 and region_code = #{regionCode, jdbcType=VARCHAR}")
    Integer checkUniqueForRegionCode(@Param("regionCode") String regionCode);

    @Select("select count(objuid) from base_region where del_flag = 0 and region_name = #{regionName, jdbcType=VARCHAR}")
    Integer checkUniqueForRegionName(@Param("regionName") String regionName);

    @Select("select count(objuid) from base_region where del_flag = 0 and region_code = #{regionCode, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForRegionCodeUpdate(@Param("regionCode") String regionCode, @Param("id") String id);

    @Select("select count(objuid) from base_region where del_flag = 0 and region_name = #{regionName, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForRegionNameUpdate(@Param("regionName") String regionName, @Param("id") String id);
}
